//
//  FFNavigationController.m
//  FFStory
//
//  Created by BaeCheung on 14/11/12.
//  Copyright (c) 2014年 FF. All rights reserved.
//

#import "InteractivePop.h"
#import "FFNavigationController.h"

@interface FFNavigationController () <UIGestureRecognizerDelegate>
@property (strong, nonatomic) NSMutableArray *scrollViews;
@property (strong, nonatomic) InteractivePop *interactivePop;
@end

@implementation FFNavigationController
#pragma mark - NSObject
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}
- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass {
    if (self = [super initWithNavigationBarClass:navigationBarClass toolbarClass:toolbarClass]) {
        [self setup];
    }
    return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self setup];
    }
    return self;
}
- (void)setup {
    // 不需要导航栏
    self.navigationBar.hidden = YES;
    self.navigationBar.translucent = NO;
}
#pragma mark - UIView
- (void)viewDidLoad {
    [super viewDidLoad];
    _scrollViews = [NSMutableArray array];
    
    // 自定义的滑动手势及系统的手势
    _interactivePop = [[InteractivePop alloc] initWithNavigationController:self];
    self.interactivePopEnable = YES;
    self.delegate = _interactivePop;
    self.interactivePopGestureRecognizer.delegate = nil;
}
// 可以通过这里进行开关
- (void)setInteractivePopEnable:(BOOL)interactivePopEnable {
    _interactivePop.interactivePopEnable = interactivePopEnable;
}
// 处理scrollView
- (BOOL)shouldSimultaneouslyScrollView:(UIScrollView *)scrollView {
    return !scrollView.bounces && [_scrollViews containsObject:scrollView];
}
- (void)registerSimultaneouslyScrollView:(__unsafe_unretained UIScrollView *)scrollView {
    [_scrollViews addObject:scrollView];
}
- (void)invalidateSimultaneouslyScrollView:(__unsafe_unretained UIScrollView *)scrollView {
    [_scrollViews removeObject:scrollView];
}
#pragma mark - UIStatusBar
- (UIStatusBarStyle)preferredStatusBarStyle {
    return [[self.viewControllers lastObject] preferredStatusBarStyle];
}
- (BOOL)prefersStatusBarHidden {
    return [[self.viewControllers lastObject] prefersStatusBarHidden];
}
#pragma mark - Bar Animated
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 仅第一级需要工具栏
    if (self.viewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
    // 不需要工具栏的，确保位于视图最后层（bug：hide tabbar -> push -> pop -> show tabbar -> push）
    if (self.tabBarController)
        [self.tabBarController.view sendSubviewToBack:self.tabBarController.tabBar];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    UIViewController *vc = [super popViewControllerAnimated:animated];
    if (self.tabBarController && self.viewControllers.count == 1)
        [self.tabBarController.view bringSubviewToFront:self.tabBarController.tabBar];
    return vc;
}
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSArray *vcs = [super popToViewController:viewController animated:animated];
    if (self.tabBarController && self.viewControllers.count == 1)
        [self.tabBarController.view bringSubviewToFront:self.tabBarController.tabBar];
    return vcs;
}
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated {
    NSArray *vcs = [super popToRootViewControllerAnimated:animated];
    if (self.tabBarController)
        [self.tabBarController.view bringSubviewToFront:self.tabBarController.tabBar];
    return vcs;
}

//#pragma mark -
//#pragma mark Gesture recognizer
//
//- (void)panGestureRecognized:(UIPanGestureRecognizer *)sender
//{
//    
//}

@end
